package com.hnust.controller;

import com.hnust.domain.Student;
import com.hnust.domain.Teacher;
import com.hnust.domain.record.NewQuestionRecord;
import com.hnust.service.UpLoadService;
import com.hnust.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Author Tonited
 * @Description //文件上传的处理
 * @Date 16:24 2020/10/29
 **/
@CrossOrigin("*")
@RestController
@RequestMapping("/upload")
public class UpLoadController {

	@Autowired
	UpLoadService upLoadService;


	//上传章节的接口 平时隐藏起来
//	@PostMapping("/chapter")
//	public Result<String> uploadStudents(@RequestParam("file") MultipartFile file) {
//		String msg = "成功";
//		int code = 1;
//		try{
//			msg = upLoadService.readChapter(file, "ebb2ac4475");
//		} catch (Exception e) {
//			msg = e.getMessage();
//			code = 0;
//		}
//		return new Result<>(code,msg,null);
//	}

	/*
	 * @Author Tonited
	 * @Description //TODO 读取上传的专业毕业要求分解指标点文件并存入数据库
	 * @Date 16:25 2020/10/29
	 * @Param [file]
	 * @return com.hnust.util.Result<java.lang.Object>
	 **/
	@PostMapping("grad")
	public Result<Object> uploadGard(@RequestParam("file") MultipartFile file,@RequestParam("collegeId") String collegeId){
		String res = null;
		int flag = 0;
		try {
			res = upLoadService.readGradFile(file,collegeId);
		} catch (Exception e) {
			res = e.getMessage();
		}
		if(res.contains("成功")){
			flag=1;
		}
		return new Result<>(flag,res,null);
	}

	/*
	 * @Author Tonited
	 * @Description //TODO 读取课程目标并存入数据库，返回报错信息
	 * @Date 16:38 2020/10/29
	 * @Param [file]
	 * @return com.hnust.util.Result<java.lang.Object>
	 **/
	@PostMapping("coutar")
	public Result<Object> uploadCoutar(@RequestParam("file") MultipartFile file,@RequestParam("collegeId") String collageId){
		String res = null;
		int flag = 0;
		try {
				res = upLoadService.readCoutarFile(file,collageId);
		} catch (Exception e) {
//			res = e.getMessage();
			res = "执行出错，请联系管理员";
		}
		if(res.contains("成功")){
			flag=1;
		}
		return new Result<>(flag,res,null);
	}

	/*
	 * @Author Tonited
	 * @Description //TODO 读取批量上传学生文件并返回学生列表
	 * @Date 17:27 2020/10/30
	 * @Param [file, collageId]
	 * @return com.hnust.util.Result<java.util.List<com.hnust.domain.Student>>
	 **/
	@PostMapping("students")
	public Result<List<Student>> uploadStudents(@RequestParam("file") MultipartFile file,@RequestParam("collegeId") String collageId) {
		String msg = "成功";
		int code = 1;
		List<Student> list = null;
		try{
			Map<String, Object> res = upLoadService.readStudsent(file, collageId);
			list = (List<Student>) res.get("list");
			msg = (String) res.get("msg");
		} catch (Exception e) {
			msg = e.getMessage();
			code = 0;
		}
		return new Result<List<Student>>(code,msg,list);
	}

	/*
	 * @Author Tonited
	 * @Description //TODO 读取批量上传教师文件并返回教师列表
	 * @Date 17:27 2020/10/30
	 * @Param [file, collageId]
	 * @return com.hnust.util.Result<java.util.List<com.hnust.domain.Student>>
	 **/
	@PostMapping("teachers")
	public Result<List<Teacher>> uploadTeachers(@RequestParam("file") MultipartFile file, @RequestParam("collegeId") String collageId) {
		String msg = "成功";
		int code = 1;
		List<Teacher> list = null;
		try{
			Map<String, Object> res = upLoadService.readTeacher(file, collageId);
			list = (List<Teacher>) res.get("list");
			msg = (String) res.get("msg");
		} catch (Exception e) {
			msg = e.getMessage();
			code = 0;
		}
		return new Result<List<Teacher>>(code,msg,list);
	}

	/*
	 * @Author Tonited
	 * @Description //TODO 读取题目文件并返回题目列表
	 * @Date 17:26 2020/10/30
	 * @Param [file, courseId]
	 * @return com.hnust.util.Result<java.util.List<com.hnust.domain.record.NewQuestionRecord>>
	 **/
	@PostMapping("questions")
	public Result<List<NewQuestionRecord>> uploadQuestions(@RequestParam("file") MultipartFile file, @RequestParam("courseId") String courseId) {
		String msg = "成功";
		int code = 1;
		List<NewQuestionRecord> list = null;
		try{
			Map<String, Object> res = upLoadService.readQuestions(file, courseId);
			list = (List<NewQuestionRecord>) res.get("list");
			msg = (String) res.get("msg");
		} catch (Exception e) {
			msg = e.getMessage();
			code = 0;
		}
		return new Result<List<NewQuestionRecord>>(code,msg,list);
	}

}
